
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { HandpicDevice } from '@libs/db/models/handpic-device.model';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { IPaginationFormWithCondition } from 'types/requestParams';
import { formatAgragateCondition } from 'utils';

@Injectable()
export class HandpicDeviceService {
  constructor(@InjectModel('HandpicDevice') private readonly handpicDeviceModel: Model<HandpicDevice>) { }

  /**
   * 新增
   * @param body 
   * @returns 
   */
  async create(body: HandpicDevice) {
    try {
      const doc = await this.handpicDeviceModel.create(body);
      await doc.save()
      return '新增成功'
    } catch (e) {
      if (e.status === 400) {
        throw new HttpException(e, HttpStatus.BAD_REQUEST)
      } else {
        throw new HttpException('服务器内部错误', HttpStatus.INTERNAL_SERVER_ERROR)
      }
    }
  }

  /**
   * @breif: 根据id删除
   * @param {string} _id
   * @return {*}
   */
  async remove(_id: string) {
    try {
      await this.handpicDeviceModel.findByIdAndDelete(_id);
      return '删除成功'
    } catch (e) {
      if (e.status === 400) {
        throw new HttpException(e, HttpStatus.BAD_REQUEST)
      } else {
        throw new HttpException('服务器内部错误', HttpStatus.INTERNAL_SERVER_ERROR)
      }
    }
  }

  /**
   * @breif: 改,body 中必须要包含_id字段
   * @param {*} body
   * @return {*}
   */
  async update(body: HandpicDevice) {
    try {
      await this.handpicDeviceModel.findByIdAndUpdate(body._id, body);
      return '修改成功'
    } catch (e) {
      if (e.status === 400) {
        throw new HttpException(e, HttpStatus.BAD_REQUEST)
      } else {
        throw new HttpException('服务器内部错误', HttpStatus.INTERNAL_SERVER_ERROR)
      }
    }
  }

  /**
   * @breif: 条件查询带分页,需要根据情况填写ands
   * @param {IPaginationFormWithCondition} body
   * @return {*}
   */
  async findWidthCondition(body: IPaginationFormWithCondition) {
    try {
      const ands = [
        { deviceName: { $regex: body.deviceName ?? '' } }
      ]
      const condition = formatAgragateCondition(ands, body.pageNum, body.pageSize);
      const res = await this.handpicDeviceModel.aggregate(JSON.parse(condition));
      if (res.length) {
        res[0].rows.sort((a, b) => a.order - b.order)
        return res[0]
      } else {
        return []
      }
    } catch (e) {
      if (e.status === 400) {
        throw new HttpException(e, HttpStatus.BAD_REQUEST)
      } else {
        throw new HttpException('服务器内部错误', HttpStatus.INTERNAL_SERVER_ERROR)
      }
    }
  }

  /**
   * @breif: 获取所有设备，首页展示
   * @return {*}
   */
  async getDevices() {
    try {
      const res = await this.handpicDeviceModel.find({}).sort({ order: 1 })
      return res
    } catch (e) {
      if (e.status === 400) {
        throw new HttpException(e, HttpStatus.BAD_REQUEST)
      } else {
        throw new HttpException('服务器内部错误', HttpStatus.INTERNAL_SERVER_ERROR)
      }
    }
  }

  /**
   * @breif: 根据_id查找
   * @param {string} id
   * @return {*}
   */
  async findById(_id: string) {
    try {
      const res = await this.handpicDeviceModel.findById(_id);
      return res || '未查询到信息,请检查id是否正确'
    } catch (e) {
      if (e.status === 400) {
        throw new HttpException(e, HttpStatus.BAD_REQUEST)
      } else {
        throw new HttpException('服务器内部错误', HttpStatus.INTERNAL_SERVER_ERROR)
      }
    }
  }
}
